<?php

namespace App\GraphQL\Mutations\FactoryDevice;
use App\Models\FactoryDevice;
use App\Models\FactoryStatistical;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Facades\Validator;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
class Bind
{
    /**
     * Return a value for the field.
     *
     * @param  null  $rootValue Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param  mixed[]  $args The arguments that were passed into the field.
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context Arbitrary data that is shared between all fields of a single query.
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     * @return mixed
     */
    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        $data = $args;
        $device = $this->validate($data);
        if ($device) {
            $device->connection_password = $data['connection_password'];
            $device->save();
        } else {
            $authUser = auth()->user();
            $data['status'] = 'NOT';
            $data['user_id'] = $authUser->id;
            $device = new FactoryDevice($data);
            $device->save();
            $statistical = FactoryStatistical::firstOrCreate(['user_id'=>$authUser->id]);
            $statistical->increment('total_device');
        }
        return [
            'status'=> '200',
            'message'=>'绑定成功'
        ];
    }
    public function validate($data)
    {
        $rules = [
            'unique_id' => [
                'required'
            ],
            'mac_address' => [
                'required'
            ],
            'connection_password' => [
                'required'
            ],
        ];
        $messages = [
            'unique_id.required' => '设备列表不能为空',
            'mac_address.required' => '设备列表为数组',
            'connection_password.required' => '设备列表ID不能为空'
        ];
        $validate = Validator::make($data, $rules, $messages);
        if ($validate->fails()) {
            throw new \Exception($validate->messages()->first());
        }
        $device = FactoryDevice::where('unique_id',$data['unique_id'])->orWhere('mac_address', $data['mac_address'])->first();
        if ($device) {
            if ($device->unique_id !== $data['unique_id']){
                $oldDevice = FactoryDevice::where('mac_address', $data['mac_address'])->first();
                throw new \Exception("该unique_id已经绑定设备:{$oldDevice->mac_address}");
            }
            if ($device->mac_address !== $data['mac_address']){
                $oldDevice = FactoryDevice::where('unique_id', $data['unique_id'])->first();
                throw new \Exception("该mac_address已经绑定设备:{$oldDevice->unique_id}");
            }
            if($device->status==='READONLY'){
                throw new \Exception('设备已经出厂');
            }
            return $device;
        }
        return null;
    }
}
